import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Put,
  Headers,
  HttpStatus,
  HttpCode,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { PostsService } from './post.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { CreateReplyDto } from './dto/create-reply.dto';
import {
  ApiTags,
  ApiOperation,
  ApiParam,
  ApiBody,
  ApiBearerAuth,
  ApiResponse,
  ApiQuery,
} from '@nestjs/swagger';
import { ApiResponseDto } from '../common/dto/api-response.dto';

@ApiTags('Post')
@Controller('posts')
export class PostController {
  constructor(private readonly postService: PostsService) {}

  @Get('list')
  @ApiOperation({
    summary: '获取帖子列表',
    description: '获取帖子列表，可按节点过滤',
  })
  @ApiQuery({
    name: 'nodeId',
    description: '节点ID',
    required: false,
    type: Number,
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '成功获取帖子列表',
    type: ApiResponseDto,
  })
  async getPosts(@Query('nodeId') nodeId?: number) {
    if (nodeId) {
      return this.postService.findPostsByNode(nodeId);
    }
    return this.postService.findAllPosts();
  }

  @Get(':id')
  @ApiOperation({
    summary: '获取帖子详情',
    description: '根据帖子ID获取详细信息',
  })
  @ApiParam({ name: 'id', description: '帖子ID' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '成功获取帖子详情',
    type: ApiResponseDto,
  })
  async getPost(@Param('id', ParseIntPipe) id: number) {
    return this.postService.findPostById(id);
  }

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiBearerAuth('bearer')
  @ApiOperation({ summary: '创建帖子', description: '创建新帖子' })
  @ApiBody({ type: CreatePostDto })
  @ApiResponse({
    status: HttpStatus.CREATED,
    description: '帖子创建成功',
    type: ApiResponseDto,
  })
  async createPost(
    @Body() createPostDto: CreatePostDto,
    @Headers('authorization') auth: string,
  ) {
    const token = auth.replace('Bearer ', '');
    return this.postService.createPost(createPostDto, token);
  }

  @Put(':id')
  @ApiBearerAuth('bearer')
  @ApiOperation({ summary: '更新帖子', description: '更新指定帖子的内容' })
  @ApiParam({ name: 'id', description: '帖子ID' })
  @ApiBody({ type: UpdatePostDto })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '帖子更新成功',
    type: ApiResponseDto,
  })
  async updatePost(
    @Param('id', ParseIntPipe) id: number,
    @Body() updatePostDto: UpdatePostDto,
    @Headers('authorization') auth: string,
  ) {
    const token = auth.replace('Bearer ', '');
    return this.postService.updatePost(id, updatePostDto, token);
  }

  @Delete(':id')
  @ApiBearerAuth('bearer')
  @ApiOperation({ summary: '删除帖子', description: '删除指定帖子' })
  @ApiParam({ name: 'id', description: '帖子ID' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '帖子删除成功',
    type: ApiResponseDto,
  })
  async deletePost(
    @Param('id', ParseIntPipe) id: number,
    @Headers('authorization') auth: string,
  ) {
    const token = auth.replace('Bearer ', '');
    return this.postService.deletePost(id, token);
  }

  @Get(':id/replies')
  @ApiOperation({
    summary: '获取帖子回复',
    description: '获取指定帖子的所有回复',
  })
  @ApiParam({ name: 'id', description: '帖子ID' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '成功获取回复列表',
    type: ApiResponseDto,
  })
  async getReplies(@Param('id', ParseIntPipe) id: number) {
    return this.postService.findRepliesByPost(id);
  }

  @Post(':id/replies')
  @HttpCode(HttpStatus.CREATED)
  @ApiBearerAuth('bearer')
  @ApiOperation({ summary: '创建回复', description: '在指定帖子下创建新回复' })
  @ApiParam({ name: 'id', description: '帖子ID' })
  @ApiBody({ type: CreateReplyDto })
  @ApiResponse({
    status: HttpStatus.CREATED,
    description: '回复创建成功',
    type: ApiResponseDto,
  })
  async createReply(
    @Param('id', ParseIntPipe) id: number,
    @Body() createReplyDto: CreateReplyDto,
    @Headers('authorization') auth: string,
  ) {
    const token = auth.replace('Bearer ', '');
    createReplyDto.postId = id;
    return this.postService.createReply(createReplyDto, token);
  }
}
